var model = require('../model');
var Manager = model.Manager;

var tools = require('../common/tools');

//# 管理员信息
exports.index = function(req, res, next) {
    res.render('backend/manager/index', {
        title: '管理员信息'
    });
};


//# 管理员列表
exports.list = function(req, res, next) {
    var pageInfo = {};

    pageInfo.pageIndex = parseInt(req.query.pageIndex) || 1;
    pageInfo.pageSize = parseInt(req.query.pageSize) || 10;
    var skipIndex = (pageInfo.pageIndex - 1) * (pageInfo.pageSize);


    Manager.count({
        deleted: false,
    }, function(error, count) {
        if (error) {
            console.log(error);
        } else {
            pageInfo.count = count;
            pageInfo.totalPage = Math.ceil(count / pageInfo.pageSize);
        }
    });

    Manager.find({
            deleted: false,
        })
        .skip(skipIndex)
        .limit(pageInfo.pageSize)
        .exec(function(error, managers) {
            if (error) {
                console.log(error);
                return;
            }
            res.render('backend/manager/list', {
                managers: managers,
                pageInfo: pageInfo
            });
        });
};

//# 添加管理员界面
exports.new = function(req, res, next) {
    res.render('backend/manager/new', {
        title: '添加管理员'
    });
};

//# 创建管理员
exports.create = function(req, res, next) {

    var email = req.body.email;
    var password = req.body.password;
    var remark = req.body.remark;


    Manager.find({
        'email': email
    }, function(error, managers) {
        if (error) {
            console.log(error);
            return;
        }
        if (managers.length > 0) {
            res.json({
                success: false,
                errors: [{
                    message: "邮箱已存在"
                }]
            });
            return;
        }
        tools.bhash(password, function(error, passhash) {
            console.log(passhash);
            if (error) {
                console.log(error);
                return;
            } else {
                var manager = new Manager({
                    email: email,
                    password: passhash,
                    remark: remark,
                    deleted: false
                });
                manager.save(function(error, manager) {
                    var result = {};
                    if (error) {
                        result.success = false;
                        res.json(result);
                    } else {
                        result.success = true;
                        res.append('Location', '/backend/managers');
                        res.json(result);
                    }
                });
            }
        });
    });
};

//# 更新管理员
exports.delete = function(req, res, next) {
    var managerId = req.params.id;

    Manager.update({
        _id: managerId
    }, {
        deleted: true
    }, function(error) {
        var result = {};
        if (error) {
            result.success = false;
            res.json(result);
        } else {
            result.success = true;
            res.json(result);
        }
    });
};

//# 初始化管理员密码
//
exports.init = function(req, res, next) {
    var email = 'admin@email.com';
    var password = 'admin1q3e2w4r';

    Manager.find({
        'email': email
    }, function(error, managers) {
        if (error) {
            console.log(error);
        }
        if (managers.length > 0) {
            res.send('exists!');
        }

        tools.bhash(password, function(error, passhash) {
            console.log(passhash);
            if (error) {
                console.log(error);
            } else {
                var manager = new Manager({
                    email: email,
                    password: passhash,
                    deleted: false
                });
                manager.save(function(error, user) {
                    if (error) {
                        console.log(error);
                    }
                    res.send('ok');
                });
            }
        });
    });
};